import 'package:flutter/material.dart';
import '../models/program_block.dart';
import 'program_block_widget.dart';

/// 程序块选择面板组件
class ProgramBlocksPanel extends StatefulWidget {
  const ProgramBlocksPanel({super.key});

  @override
  State<ProgramBlocksPanel> createState() => _ProgramBlocksPanelState();
}

class _ProgramBlocksPanelState extends State<ProgramBlocksPanel> with SingleTickerProviderStateMixin {
  late TabController _tabController;
  final List<BlockType> _blockTypes = [
    BlockType.event,
    BlockType.motion,
    BlockType.control,
    BlockType.operator,
    BlockType.sensor,
  ];
  
  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: _blockTypes.length, vsync: this);
  }
  
  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }
  
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.0),
        border: Border.all(color: Colors.grey.shade300),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.2),
            spreadRadius: 1,
            blurRadius: 3,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        children: [
          // 标题
          Container(
            padding: const EdgeInsets.all(12.0),
            decoration: BoxDecoration(
              color: Colors.blue.shade100,
              borderRadius: const BorderRadius.only(
                topLeft: Radius.circular(8.0),
                topRight: Radius.circular(8.0),
              ),
            ),
            child: Row(
              children: [
                const Icon(Icons.widgets, color: Colors.blue),
                const SizedBox(width: 8.0),
                Text(
                  '程序块',
                  style: Theme.of(context).textTheme.titleMedium,
                ),
              ],
            ),
          ),
          
          // 分类标签
          TabBar(
            controller: _tabController,
            isScrollable: true,
            labelColor: Colors.blue,
            unselectedLabelColor: Colors.grey,
            tabs: _blockTypes.map((type) {
              IconData icon;
              String label;
              
              switch (type) {
                case BlockType.event:
                  icon = Icons.flag;
                  label = '事件';
                  break;
                case BlockType.motion:
                  icon = Icons.directions_run;
                  label = '运动';
                  break;
                case BlockType.control:
                  icon = Icons.loop;
                  label = '控制';
                  break;
                case BlockType.operator:
                  icon = Icons.calculate;
                  label = '运算';
                  break;
                case BlockType.sensor:
                  icon = Icons.sensors;
                  label = '传感器';
                  break;
              }
              
              return Tab(
                icon: Icon(icon),
                text: label,
              );
            }).toList(),
          ),
          
          // 程序块列表
          Expanded(
            child: TabBarView(
              controller: _tabController,
              children: _blockTypes.map((type) {
                final blocks = ProgramBlocks.getBlocksByType(type);
                
                return blocks.isEmpty
                    ? const Center(child: Text('暂无可用程序块'))
                    : ListView.builder(
                        padding: const EdgeInsets.all(8.0),
                        itemCount: blocks.length,
                        itemBuilder: (context, index) {
                          final block = blocks[index];
                          return Padding(
                            padding: const EdgeInsets.symmetric(vertical: 4.0),
                            child: ProgramBlockWidget(
                              block: block.clone(),
                              isDraggable: true,
                              onTap: (_) {},
                            ),
                          );
                        },
                      );
              }).toList(),
            ),
          ),
        ],
      ),
    );
  }
} 